package com.sky.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.dto.SetmealDTO;
import com.sky.dto.SetmealPageQueryDTO;
import com.sky.entity.Dish;
import com.sky.entity.Setmeal;
import com.sky.entity.SetmealDish;
import com.sky.exception.BaseException;
import com.sky.mapper.SetmealDishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.result.PageResult;
import com.sky.service.SetmealService;
import com.sky.vo.DishItemVO;
import com.sky.vo.SetmealVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * ClassName: SetmealServiceImpl
 * Package: com.sky.service.impl
 * Description:
 *
 * @Author 八云
 * @Create 2025/3/20 21:34
 * @Version 1.0
 */
@Service
public class SetmealServiceImpl implements SetmealService {

    @Autowired
    private SetmealMapper setmealMapper;

    @Autowired
    private SetmealDishMapper setmealDishMapper;


    /**
     * 分页查询
     *
     * @param setmealPageQueryDTO
     * @return
     */
    @Override
    public PageResult pageQuery(SetmealPageQueryDTO setmealPageQueryDTO) {
        PageHelper.startPage(setmealPageQueryDTO.getPage(), setmealPageQueryDTO.getPageSize());
        Page<SetmealVO> setmealVOS = setmealMapper.pageQuery(setmealPageQueryDTO);
        return new PageResult(setmealVOS.getTotal(), setmealVOS.getResult());

    }

    /**
     * 新增套餐
     *
     * @param setmealDTO
     */
    @Override
    @Transactional
    public void saveWithDish(SetmealDTO setmealDTO) {
        Setmeal setmeal = new Setmeal();
        BeanUtils.copyProperties(setmealDTO, setmeal);
        // 新增套餐
        setmealMapper.save(setmeal);
        // 获取套餐id
        Long setmealId = setmeal.getId();

        // 保存套餐和菜品的关联关系
        List<SetmealDish> setmealDishes = setmealDTO.getSetmealDishes();
        if(setmealDishes != null && setmealDishes.size() > 0){
            setmealDishes.forEach(setmealDish -> {
                setmealDish.setSetmealId(setmealId);
            });
            // 批量插入套餐和菜品的关联关系
            setmealDishMapper.saveDishes(setmealDishes);
        }

    }

    @Override
    public SetmealDTO getByIdWithDish(Long id) {
        SetmealDTO setmealDTO = setmealMapper.getByIdWithDish(id);
        return setmealDTO;
    }

    // 修改套餐
    @Override
    @Transactional
    public void updateWithDish(SetmealDTO setmealDTO) {
        // 修改前删除套餐和菜品的关联关系
        setmealDishMapper.deleteBySetmealId(setmealDTO.getId());

        Setmeal setmeal = new Setmeal();
        BeanUtils.copyProperties(setmealDTO, setmeal);

        // 修改套餐
        setmealMapper.update(setmeal);

        // 保存套餐和菜品的关联关系
        List<SetmealDish> setmealDishes = setmealDTO.getSetmealDishes();
        if(setmealDishes != null && setmealDishes.size() > 0){
            setmealDishes.forEach(setmealDish -> {
                setmealDish.setSetmealId(setmeal.getId());
            });
            // 批量插入套餐和菜品的关联关系
            setmealDishMapper.saveDishes(setmealDishes);
        }
    }

    /**
     * 停售起售套餐
     * @param status
     * @param id
     */
    @Override
    public void startOrStop(Integer status, Long id) {
        System.out.println("status:" + status);
        setmealMapper.startOrStop(status,id);
    }

    /**
     * 批量删除套餐
     * @param ids
     */
    @Override
    @Transactional
    public void deleteBatch(List<Long> ids) {
        // 先判断套餐状态
        for (Long id : ids) {
            Dish dish = setmealMapper.getById(id);
            if(dish.getStatus() == 1){
                // 套餐正在售卖中，不能删除
                throw new BaseException(dish.getName() + "套餐正在售卖中，不能删除");
            }
        }
        // 根据套餐ID，先删除菜单信息
        setmealDishMapper.deleteByDishId(ids);
        // 再删除套餐信息
        setmealMapper.deleteBatch(ids);
    }

    /**
     * 根据套餐分类id查询起售中套餐
     * @param setmeal
     * @return
     */
    @Override
    public SetmealVO[] list(Setmeal setmeal) {
        return setmealDishMapper.getBySetmealId(setmeal);
    }

    /**
     * 根据套餐id查询菜品选项
     * @param id
     * @return
     */
    @Override
    public DishItemVO[] getByIdWithDishItem(Long id) {
        return setmealDishMapper.getByIdWithDishItem(id);
    }

    /**
     * 根据套餐id查询套餐状态
     *
     * @param setmealId
     * @param status
     * @return
     */
    @Override
    public String getByIdStatus(Long setmealId, Integer status) {
        return setmealMapper.getByIdStatus(setmealId,status);
    }


}
